"""
@version:
@filename : 链表反转
@author : liuhongjie
@projectname: day02
@time: 2022/05/30
"""


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        # 判断特殊情况
        if not head or not head.next:
            return head
        pre = None
        while head:
            # 暂存当前node的下一个节点；
            next_node = head.next
            # 当前node的下一个节点指向前一个node
            head.next = pre
            # 更新，当前node为前一个节点
            pre = head
            # 更新，下一个节点为当前node
            head = next_node
        return pre




